/**
* @project admin-go-api
* @name encoding
* @descriptions 字符编码转换
* @author 寒锋
* @date 2024-01-10 14:05:15
 */

package util

import (
	"bytes"
	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/transform"
	"io/ioutil"
)

// GbkToUtf8 GBK转UTF-8
func GbkToUtf8(str []byte) ([]byte, error) {
	reader := transform.NewReader(bytes.NewReader(str), simplifiedchinese.GBK.NewDecoder())
	data, err := ioutil.ReadAll(reader)
	if err != nil {
		return nil, err
	}
	return data, nil
}

// Utf8ToGbk UTF-8转GBK
func Utf8ToGbk(str []byte) ([]byte, error) {
	reader := transform.NewReader(bytes.NewReader(str), simplifiedchinese.GBK.NewEncoder())
	data, err := ioutil.ReadAll(reader)
	if err != nil {
		return nil, err
	}
	return data, nil
}
